package org.itboys.core.controller.web.coupon;

import org.itboys.core.entity.mysql.coupon.SundayCoupon;
import org.itboys.core.service.mysql.web.SundayCouponService;
import org.itboys.core.util.MybatisParamUtil;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/12/10
 * @Description：优惠券
 */
@Controller
@RequestMapping("/sunday/web/coupon")
public class SundayCouponController extends BaseController {
    @Autowired
    private SundayCouponService couponService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "/sunday/coupon/index";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response) {
        MybatisParamUtil paramUtil = new MybatisParamUtil(request);
        PageResult<SundayCoupon> result = couponService.select(paramUtil);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response
            , @ModelAttribute SundayCoupon coupon
            , @RequestParam(value = "file",required = false) MultipartFile file
    ) {
        couponService.save(coupon,file);
        ResponseMessage.success();
    }
    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request, HttpServletResponse response
            , @RequestParam("id")Long id
    ) {
        ResponseMessage.success(couponService.findOne(id));
    }
}

